<?php
namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/*芝麻信用*/

class ZhimaH5Controller extends Controller
{	
	
	   private $client=null;
	   
	    //芝麻信用网关地址
	   public $gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";

       //商户私钥
       public $privateKeyFile='./Public/rsa/rsa_private_key.pem';
       //支付宝公钥
       public $zmPublicKeyFile='./Public/rsa/rsa_public_key.pem';

	    //数据编码格式
	    public $charset = "UTF-8";

	    //芝麻分配给商户的 appId
	    public $appId = "1004308";
		
		public function _initialize()
		{   
			header("Content-type: text/html; charset=utf-8");
			vendor("alipay-zmxy-h5-sdk.zmop.ZmopClient"); 

			if (is_null($client)) 
			{
				$this->client = new \ZmopClient($this->gatewayUrl,$this->appId,$this->charset,$this->privateKeyFile,$this->zmPublicKeyFile);	
			}
			
		}

		/*发起授权*/
		public function ZhimaAuthInfoAuthorize($user_id,$name,$certNo)
		{	
			vendor("alipay-zmxy-h5-sdk.zmop.request.ZhimaAuthInfoAuthorizeRequest");

	         $request = new \ZhimaAuthInfoAuthorizeRequest();

	    
	         $request->setChannel("apppc");
	         $request->setPlatform("zmop");
	         $request->setIdentityType("2");// 必要参数 
	         $request->setIdentityParam("{\"name\":\"".$name."\",\"certType\":\"IDENTITY_CARD\",\"certNo\":\"".$certNo."\"}");// 必要参数 
	         $request->setBizParams("{\"auth_code\":\"M_APPSDK\",\"channelType\":\"appsdk\",\"state\":\"".$user_id."\"}");// 

	         $url = $this->client->generatePageRedirectInvokeUrl($request);
	       
		     return $url;
			 
			Header("Location:$url");
	         //echo $url;
	    }
		
		/*芝麻分*/
		public function zhimaCreditScoreGet($openId)
		{	
			vendor("alipay-zmxy-h5-sdk.zmop.request.ZhimaCreditScoreGetRequest");

			 $transactionId=payJDSn();

	         $request = new \ZhimaCreditScoreGetRequest();
	         $request->setChannel("apppc");
	         $request->setPlatform("zmop");
	         $request->setTransactionId($transactionId);// 必要参数 
	         $request->setProductCode("w1010100100000000001");// 必要参数 
	         $request->setOpenId($openId);// 必要参数 
	         $response = $this->client->execute($request);
	         
	         return $response;
        }


       

	     /**
	      *  人脸识别 步骤一 
	      */
       public function zhimaCustomerCertificationInitialize($cert_name,$cert_no)
       {	
       		vendor("alipay-zmxy-h5-sdk.zmop.request.ZhimaCustomerCertificationInitializeRequest");
       		 
       		 $ransactionId=date('YmdHis').snMaker();

	         $request = new \ZhimaCustomerCertificationInitializeRequest();
	         $request->setPlatform("zmop");
	         $request->setTransactionId($ransactionId);// 必要参数 
	         $request->setProductCode("w1010100000000002978");// 必要参数 
	         $request->setBizCode("FACE");// 必要参数 
	         $request->setIdentityParam("{\"identity_type\": \"CERT_INFO\", \"cert_type\": \"IDENTITY_CARD\", \"cert_name\": \"".$cert_name."\", \"cert_no\":\"".$cert_no."\"}");// 必要参数 
	         $request->setMerchantConfig("{\"need_user_authorization\":\"false\"}");// 
	         $request->setExtBizParam("{}");// 必要参数 
	         $response = $this->client->execute($request);

	        return $response;
       }


       /*人脸识别步骤二*/
       public function ZhimaCustomerCertificationCertify($setBizNo)
       {
	         vendor("alipay-zmxy-h5-sdk.zmop.request.ZhimaCustomerCertificationCertifyRequest");

	         $request = new \ZhimaCustomerCertificationCertifyRequest();
	         $request->setChannel("apppc");
	         $request->setPlatform("zmop");
	         $request->setBizNo($setBizNo);// 必要参数 
	         $request->setReturnUrl("alipays://n.lvyuecp.com/Api/Zm/ali_notify");// 必要参数 
	         $url = $this->client->generatePageRedirectInvokeUrl($request);

	         Header("Location:$url");
      }


  
      public function test()
      {
      	$res=$this->zhimaCreditWatchlistiiGet('268819179235638653398843632');

      	print_r($res);
      }
	
	
      /*行业关注名单
	   * $openId
      */
	 public function zhimaCreditWatchlistiiGet($openId)
	 {	
	 		vendor("alipay-zmxy-h5-sdk.zmop.request.ZhimaCreditWatchlistiiGetRequest");
	 		
	 		$transactionId=payJDSn();
	 		//$openId='268819179235637683069131971';

	        $request = new \ZhimaCreditWatchlistiiGetRequest();
	        $request->setChannel("apppc");
	        $request->setPlatform("zmop");
	        $request->setProductCode("w1010100100000000022");// 必要参数 
	        $request->setTransactionId($transactionId);// 必要参数 
	        $request->setOpenId($openId);// 必要参数 
	        $response = $this->client->execute($request);
	        return $response;
	        //echo json_encode($response);
     }



     /**
      * 解析异步返回值
      */
     public function parse_notify($params,$sign)
     {
     	 $res=$this->client->decryptAndVerifySign($params,$sign);
     	 return $res;
     }


}